package com.ssh;

public class AddTwoNumbers2_2 {

	public static void main(String[] args) {
		ListNode l1 = new ListNode(8);
		ListNode l2 = new ListNode(9);
		ListNode l3 = new ListNode(9);
		ListNode l4 = new ListNode(2);
		ListNode l5 = new ListNode(6);
		ListNode l6 = new ListNode(4);
		l1.next = l2;
		l2.next = l3;
		/*
		l4.next = l5;
		l5.next = l6;*/
		
		ListNode l = addTwoNumbers(l1, l4);
		while (l.next != null) {
			System.out.println(l.val);
			l = l.next;
		}
		System.out.println(l.val);
	}
	
	public static ListNode addTwoNumbers (ListNode l1, ListNode l2) {
		ListNode pre = new ListNode(0);
		ListNode head = pre;
		int t = 0;
		while (l1!=null || l2!=null || t!=0) {
			ListNode ln = new ListNode(0);
			int sum = (l1==null ? 0 :l1.val) + (l2==null ? 0 :l2.val) + t;
			t = sum / 10;
			ln.val = sum % 10;
			pre.next = ln;
			pre = ln;
			l1 = (l1 == null ? l1 :l1.next);
			l2 = (l2 == null ? l2 :l2.next);
		}
		return head.next;
	}
}
